package cn.jdy.task.service.classify;


import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.service.OrganService;
import cn.jdy.common.util.XMLUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.ApplyCash;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.ApplyCashService;
import cn.jdy.plugins.service.CRateOrderService;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.MemberService;
import cn.jdy.plugins.service.PointRecordService;
import cn.jdy.plugins.service.ProductxService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.service.WebsiteService;
import cn.jdy.plugins.wap.util.ChatUtil;
import cn.jdy.plugins.wap.util.HttpUtils;
import cn.jdy.plugins.wap.util.RedPacketUtil;
import cn.jdy.task.service.TaskGroup;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;




@TaskGroup("提现情况")
public class Websitetx implements Job {
	@Autowired
	private WebsiteService websiteService;
	@Autowired
	private CRateOrderService cRateOrderService;
	@Autowired
	private ProductxService productxService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PointRecordService pointRecordService;
	@Autowired
	private ApplyCashService applyCashService;
	@Autowired
	private WatchUserService watchUserService;

	@Autowired
	private OrganService organService;
	@Autowired
	private LiveService liveService;

	//企业付款接口地址
	private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; 
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			System.out.println("执行了一次");
		
			
		
			//查询所有用户
			
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "invoke", e);
		}
	}
	
	
	
public static void main(String[] args) throws ParseException {
 
      /*** 

	String result = "提现成功!";
	String statusCode = "2";  //1-成功 2-失败
	List<ApplyCash> alist=applyCashService.find("status","1");
	
	for (ApplyCash cash : alist) {
		

	String trade_no = ChatUtil.create_timestamp(); //商户订单号
	try {
		//获取当前看用户信息
		Member user = memberService.find(cash.getUserOpenId());
		if(null != user){
			Organ organ = organService.find("40288119669feb25016717b91a6a0001");

			SortedMap<Object, Object> parm = new TreeMap<Object, Object>();
			parm.put("mch_appid", organ.getAppId()); //公众账号appid
            parm.put("mchid", organ.getMchid()); //商户号
            parm.put("nonce_str", RedPacketUtil.pay_nonce_str()); //随机字符串
            parm.put("partner_trade_no", trade_no); //商户订单号
            parm.put("openid", user.getOpenId()); //用户openid  
            parm.put("check_name", "NO_CHECK"); //校验用户姓名选项:NO_CHECK-不校验真实姓名 OPTION_CHECK,FORCE_CHECK-强校验真实姓名
 
            parm.put("amount",  RedPacketUtil.get_total_fee(cash.getTotal())); //转账金额,单位为分  单笔最小金额默认为1元
            parm.put("desc", "提现"); //企业付款描述信息
            parm.put("spbill_create_ip", RedPacketUtil.getHostAddress()); //服务器Ip地址
            String sign = RedPacketUtil.createSign("UTF-8", parm, organ.getApikey());
            parm.put("sign", sign);
			
            String requestXML = RedPacketUtil.getRequestXml(parm);

            	if(null != organ){
          
            		String path =  this.getClass().getClassLoader().getResource("").getPath();
            		    System.out.println(path);
            		    String pata1=path.split("/kyljadmin/")[0];
            		    String currentPath = pata1+organ.getDiploma();
            	//	 
            		    System.out.println(currentPath);
            		//    String currentPath = new File(path).getParent()+organ.getDiploma();
            		 	String xmlStr =  HttpUtils.posts(organ.getMchid(),currentPath,TRANSFERS_PAY,requestXML);
	       		  	    if(xmlStr.indexOf("SUCCESS") != -1) {   
	       		        	  Map<String, String> map = XMLUtil.doXMLParse(xmlStr); 
	       		        	  String result_code = map.get("result_code");
	       		        	  if(StringUtil.isNotEmpty(result_code) && result_code.equals("SUCCESS")){
	       		        		  statusCode = "1"; 
	       		        	  	  String partner_trade_no = map.get("partner_trade_no");  //商户订单号 
	       		        		  String payment_no = map.get("payment_no");      //企业付款成功，返回的微信订单号
	       		        		  String payment_time = map.get("payment_time");  //企业付款成功时间
	       		        		  
	       		        		  user.setKtxmoney(user.getKtxmoney()+cash.getTotal());
	       		        		  //当前余额
	       		        		  double currentMoney =  RedPacketUtil.sub(user.getMoney(), cash.getTotal());
	       		        		  
	       		        	
	       		        		  cash.setStatus("2");
	       		        		applyCashService.update(cash);
	       		   
	       		   
	       		  				  user.setMoney(currentMoney);
	       		  				  user.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
	       		  				  watchUserService.update(user);
	       		        		  
	       		        	  }else{
	       		        		  String err_code_des = map.get("err_code_des");
	       		        		  result = err_code_des;  
	       		        	  }
	       		          }else{
	       		        	System.out.println("系统繁忙,请稍后再试!");
	       		          }
            	}else{
            		System.out.println("获取商户证书信息失败,请稍后重试！");
            	}
           
	  	  if(statusCode.equals("2")){  //失败时
			cash.setOrderNo(trade_no);
				cash.setStatus("3");   //1-申请提现 ，2-成功， 3-失败
				cash.setTotal(user.getTotal());
				cash.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				applyCashService.save(cash);
			}
			
		}else{
			result = "获取用户信息失败,请稍后重试！";
		}
		
		
	} catch(Exception e) {
		BaseLog.e(this.getClass(), "修改异常", e);
		result = "连接失败，请稍后重试！";
	}
	}*/
}
}
